<!--********************************************************************
* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!--********************************************************************
* 该示例需要引入 
* Echarts (https://github.com/apache/echarts)
* ECharts-GL (https://github.com/ecomfe/echarts-gl)
*********************************************************************-->
<html>
<head>
    <meta charset='utf-8'/>
    <title data-i18n="resources.title_flightPath"></title>
    <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no'/>
    <style>
        body {
            margin: 0;
            padding: 0;
        }

        #map {
            position: absolute;
            top: 0;
            bottom: 0;
            width: 100%;
        }
    </style>
</head>
<body>
<div id='map'></div>
<script type="text/javascript" include="bootstrap" src="../js/include-web.js"></script>
<script type="text/javascript" include="echarts,echarts-gl" src="../../dist/mapboxgl/include-mapboxgl.js"></script>
<script type="text/javascript">
    // 数据来自 https://uber.github.io/deck.gl/#/examples/core-layers/line-layer
    var data;
    var attribution = "<a href='https://www.mapbox.com/about/maps/' target='_blank'>© Mapbox </a>" +
        "| Image <span>© <a href='http://support.supermap.com.cn/product/iServer.aspx' target='_blank'>SuperMap iServer</a> | </span>" +
        "<a href='https://echarts.baidu.com' target='_blank'>© 2018 " + resources.title_3baidu + " ECharts Echarts-gl</a>";
    var host = window.isLocal ? window.server : "https://iserver.supermap.io";
    var tileURL = host + "/iserver/services/map-china/rest/maps/ChinaDark/zxyTileImage.png?z={z}&x={x}&y={y}";
    var dataFile = "../data/flightpath.txt";

    var myChart = echarts.init(document.getElementById('map'));
    //获取mapbox对象


    $.get(dataFile, function (text) {

        var data = decodeFlightPathData(text);

        var dataAll = [];
        for (var i = 0; i < 4; i++) {
            dataAll = dataAll.concat(data.map(function (item) {
                return {
                    name: item.name,
                    coords: item.coords.map(function (coord) {
                        return coord.slice();
                    })
                };
            }));
        }

        myChart.setOption({
            mapbox: {
                center: [0, 51.5],
                zoom: 8,
                pitch: 60,
                altitudeScale: 5,
                style: {
                    "version": 8,
                    "sources": {
                        "raster-tiles": {
                            "attribution": attribution,
                            "type": "raster",
                            "tiles": [tileURL],
                            "tileSize": 256,
                        },
                    },
                    "layers": [{
                        "id": "simple-tiles",
                        "type": "raster",
                        "source": "raster-tiles",
                        "minzoom": 0,
                        "maxzoom": 22
                    }]
                },
                postEffect: {
                    enable: true,
                    bloom: {
                        intensity: 0.4
                    }
                }
            },
            series: [{
                type: 'lines3D',

                coordinateSystem: 'mapbox',

                effect: {
                    show: true,
                    constantSpeed: 40,
                    trailWidth: 2,
                    trailLength: 0.15,
                    trailOpacity: 1
                },

                blendMode: 'lighter',

                polyline: true,

                lineStyle: {
                    width: 1,
                    color: 'rgb(50, 60, 170)',
                    opacity: 0.1
                },

                data: dataAll
            }]

        });

        window.addEventListener('keydown', function () {
            myChart.dispatchAction({
                type: 'lines3DToggleEffect',
                seriesIndex: 0
            });
        });

        //获取mapbox对象
        if (myChart.getModel()) {
            var mapbox = myChart.getModel().getComponent('mapbox3D').getMapbox();
            mapbox.addControl(new mapboxgl.NavigationControl(), 'top-left');
        }

    });


    function decodePolyline(str, precision) {
        var index = 0;
        var lat = 0;
        var lng = 0;
        var coordinates = [];
        var shift = 0;
        var result = 0;
        var byte = null;
        var latitude_change;
        var longitude_change;
        var factor = Math.pow(10, precision || 5);
        while (index < str.length) {
            byte = null;
            shift = 0;
            result = 0;

            do {
                byte = str.charCodeAt(index++) - 63;
                result |= (byte & 0x1f) << shift;
                shift += 5;
            } while (byte >= 0x20);

            latitude_change = ((result & 1) ? ~(result >> 1) : (result >> 1));

            shift = result = 0;

            do {
                byte = str.charCodeAt(index++) - 63;
                result |= (byte & 0x1f) << shift;
                shift += 5;
            } while (byte >= 0x20);

            longitude_change = ((result & 1) ? ~(result >> 1) : (result >> 1));

            lat += latitude_change;
            lng += longitude_change;

            coordinates.push([lng / factor, lat / factor]);
        }

        return coordinates;
    }

    function decodeFlightPathData(text) {
        var lines = text.split('\n');

        var result = [];

        lines.forEach(function (line) {

            if (!line) {
                return;
            }

            var parts = line.split('\t');
            var coords0 = parts[2].split('\x01').map(function (str) {
                return decodePolyline(str, 5)
            });
            var coords1 = parts[3].split('\x01').map(function (str) {
                return decodePolyline(str, 1)
            });

            var coords = [];
            coords0.forEach(function (lineStr, i) {
                for (var j = 1; j < lineStr.length; j++) {
                    var prevPt0 = coords0[i][j - 1],
                        prevPt1 = coords1[i][j - 1],
                        currPt0 = coords0[i][j],
                        currPt1 = coords1[i][j];

                    coords.push(
                        [prevPt0[0], prevPt0[1], prevPt1[0]],
                        [currPt0[0], currPt0[1], currPt1[0]]
                    );
                }
            });

            result.push({
                name: parts[0],
                country: parts[1],
                coords: coords
            });
        });
        return result;
    }

</script>
</body>
</html>